<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/4/15 0015
 * Time: 16:52
 */

namespace Admin\Controller;

use Think\Controller;
use Think\Page;
use Think\Upload;


class SchoolMagController extends Controller
{

    //班级管理
    public function classMag()
    {
        $deptid = I("get.deptid");
        $classinfo = M("classinfo");
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        if ($deptid == 0) {
            $ret = $classinfo->order("regyear,did,cid")->page($p . ',10')->select();
            $count = $classinfo->count();
            $Page = new Page($count, 10);
            $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
            $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
            $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
            $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
            $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
            $show = $Page->show();
            $this->assign('page', $show);

            $this->assign("ret", $ret);
        } else {
            $ret = $classinfo->where("did=$deptid")->order("regyear,did,cid")->page($p . ',10')->select();

            $count = $classinfo->where("did=$deptid")->count();
            $Page = new Page($count, 10);
            $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
            $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
            $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
            $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
            $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
            $show = $Page->show();
            $this->assign('page', $show);

            $this->assign("ret", $ret);
        }
        $this->display();
    }

    //系部管理
    public function deptMag()
    {
        $deptinfo = M("deptinfo");
        $ret = $deptinfo->order("did")->select();
        $this->assign("ret", $ret);
        $this->display();
    }

    public function addClass()
    {
        if (IS_POST) {
            $ClassInfo = D("ClassInfo");
            if ($ClassInfo->create()) {
                if ($ClassInfo->add()) {
                    $this->success("添加成功", U("Admin/SchoolMag/classMag"));
                } else {
                    $this->error("添加失败");
                }
            }
        } else {
            $this->display();
        }
    }

    public function addDept()
    {
        if (IS_POST) {
            $DeptInfo = D("DeptInfo");
            if ($DeptInfo->create()) {
                if ($DeptInfo->add()) {
                    $this->success("添加成功", U("Admin/SchoolMag/deptMag"));
                } else {
                    $this->error("添加失败");
                }
            }
        } else {
            $this->display();
        }
    }

    public function delClass()
    {
        $id = I("get.id");
        $ClassInfo = D("ClassInfo");
        if ($ClassInfo->where("id={$id}")->delete()) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function delDept()
    {
        $id = I("get.id");
        $DeptInfo = D("DeptInfo");
        if ($DeptInfo->where("id={$id}")->delete()) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    /**
     * 导入班级
     * @throws \PHPExcel_Reader_Exception
     */
    public function importClass()
    {
        import("Org.Util.PHPExcel");
        $ClassInfo = D("ClassInfo");
        $cnt = 0;
        $upload = use_upload();
        $file = "./UPLOADS/" . $upload["class"]["savepath"] . $upload["class"]["savename"];
        $fileType = \PHPExcel_IOFactory::identify($file);
        $Reader = \PHPExcel_IOFactory::createReader($fileType);
        $PHPExcel = $Reader->load($file);
        $sheet = $PHPExcel->getSheet(0);
        foreach ($sheet->getRowIterator() as $row) {
            if ($row->getRowIndex() < 2) {
                continue;
            }
            $str = '';
            echo "";
            foreach ($row->getCellIterator() as $cell) {
                $str .= $cell->getValue() . '|*|';

            }
            $strs = explode("|*|", $str);
            $post["cid"] = $strs[0];
            $post["name"] = $strs[1];
            $post["did"] = getDeptId($strs[2]);
            $post["regyear"] = $strs[3];
            $ret = $ClassInfo->add($post, array(), true);
            if ($ret) {
                $cnt += 1;
            } else {
                $this->error("文件中存在错误");
            }
        }
        $this->success("有" . $cnt . "条数据导入成功");
    }

    /**
     * 导入系部
     * @throws \PHPExcel_Reader_Exception
     */
    public function importDept()
    {
        import("Org.Util.PHPExcel");
        $DeptInfo = D("DeptInfo");
        $cnt = 0;
        $upload = use_upload();
        $file = "./UPLOADS/" . $upload["dept"]["savepath"] . $upload["dept"]["savename"];
        $fileType = \PHPExcel_IOFactory::identify($file);
        $Reader = \PHPExcel_IOFactory::createReader($fileType);
        $PHPExcel = $Reader->load($file);
        $sheet = $PHPExcel->getSheet(0);
        foreach ($sheet->getRowIterator() as $row) {
            if ($row->getRowIndex() < 2) {
                continue;
            }
            $str = '';
            echo "";
            foreach ($row->getCellIterator() as $cell) {
                $str .= $cell->getValue() . '|*|';

            }
            $strs = explode("|*|", $str);
            $post["did"] = $strs[0];
            $post["name"] = $strs[1];
            $post["description"] = $strs[2];
//            dump($strs);
            $ret = $DeptInfo->add($post, array(), true);
            if ($ret) {
                $cnt += 1;
            } else {
                $this->error("文件中存在错误");
            }
        }
        $this->success("有" . $cnt . "条数据导入成功");
    }

    //导出赛项信息
    public function outputClass()
    {
        import("Org.Util.PHPExcel");
        $ClassInfo = D("ClassInfo");
        $ret = $ClassInfo->order("did,cid")->select();
        $PHPExcel = new \PHPExcel();
        $Sheet = $PHPExcel->getActiveSheet();
        $Sheet->getDefaultStyle()->getFont()->setName("宋体")->setSize(14);
        $Sheet->getDefaultStyle()
            ->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $Sheet->getColumnDimension('A')->setWidth(5);
        $Sheet->getColumnDimension('B')->setAutoSize(true);
        $Sheet->getColumnDimension('C')->setWidth(30);
        $Sheet->getColumnDimension('D')->setWidth(30);
        $Sheet->getColumnDimension('E')->setAutoSize(true);
        $Sheet->setCellValue("A1", "序号")
            ->setCellValue("B1", "班级编号")
            ->setCellValue("C1", "班级名称")
            ->setCellValue("D1", "系部名称")
            ->setCellValue("E1", "年级")
            ->getStyle("A1:E1")
            ->getFont()
            ->setName("微软雅黑")
            ->setSize(14)
            ->setBold(true);
        $startcell = 2;
        $i = 1;
        foreach ($ret as $rows) {
            $Sheet->getStyle("B" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
            $Sheet->getStyle("E" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
            $Sheet->setCellValue("A" . $startcell, $i++)
                ->setCellValueExplicit("B" . $startcell, $rows["cid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue("C" . $startcell, $rows["name"])
                ->setCellValue("D" . $startcell, getDeptName($rows["did"]))
                ->setCellValueExplicit("E" . $startcell, $rows["regyear"], \PHPExcel_Cell_DataType::TYPE_STRING);
            $startcell++;
        }
    }

    //导出系部信息
    public function outputDept()
    {
        import("Org.Util.PHPExcel");
        $DeptInfo = D("DeptInfo");
        $ret = $DeptInfo->order("did")->select();
        $PHPExcel = new \PHPExcel();
        $Sheet = $PHPExcel->getActiveSheet();
        $Sheet->getDefaultStyle()->getFont()->setName("宋体")->setSize(14);
        $Sheet->getDefaultStyle()
            ->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $Sheet->getColumnDimension('A')->setWidth(5);
        $Sheet->getColumnDimension('B')->setWidth(10);
        $Sheet->getColumnDimension('C')->setWidth(30);
        $Sheet->setCellValue("A1", "序号")
            ->setCellValue("B1", "系部编号")
            ->setCellValue("C1", "系部名称")
            ->getStyle("A1:C1")
            ->getFont()
            ->setName("微软雅黑")
            ->setSize(14)
            ->setBold(true);
        $startcell = 2;
        $i = 1;
        foreach ($ret as $rows) {
            $Sheet->getStyle("B" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
            $Sheet->setCellValue("A" . $startcell, $i++)
                ->setCellValueExplicit("B" . $startcell, $rows["did"], \PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue("C" . $startcell, $rows["name"]);
            $startcell++;
        }

        $obj = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel5");
        browser_export("学院系部信息表");
        $obj->save("php://output");
    }

}
